package com.alex.xmlyfm.client;

import com.alex.mylibrary.utils.HttpTools;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-20.
 */
public class ClientAPI {

    // 单独提取服务器地址部分 避免以后更换服务器地址
    private static final String API_POINT = "http://mobile.ximalaya.com";

    private ClientAPI(){

    }

    // ----------------------
    // 接口 12

    /**
     * 获取发现分类<br/>
     * API 地址 /mobile/discovery/v1/categories?device=android&picVersion=10&scale=2
     *
     * @return jsonObject
     */
    public static JSONObject getDiscoverCategories(){
        JSONObject ret = null;

        byte[] data =
                HttpTools.doGet(API_POINT + "/mobile/discovery/v1/categories?device=android&picVersion=10&scale=2");
        if (data != null) {
            try {
                String str = new String(data,"utf-8");
//                Log.d("clientAPI====",str);
                ret = new JSONObject(str);

            } catch (UnsupportedEncodingException | JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }

    // ----------------------
    // 接口11 用于

    /**
     * 获取发现部分 推荐列表的内容
     * /mobile/discovery/v1/recommends?channel=and-f6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.1.7.1
     *
     * @param channel 软件发布渠道
     * @param includeActivity 是否包含活动
     * @param includeSpecial 是否包含 "精品听单"
     * @return 返回 json数据
     */
    public static JSONObject getDiscoverRecommend(String channel,
                                                  boolean includeActivity,
                                                  boolean includeSpecial
                                                  ){
        JSONObject ret = null;

        if (channel == null){
            channel = "and-f6";
        }

        String url = API_POINT + "/mobile/discovery/v1/recommends"
                + "?channel=" + channel
                + "&device=android"
                + "&includeActivity=" + includeActivity
                + "&includeSpecial=" + includeSpecial
                + "&scale=2" + "&version=4.1.7.1"
                ;

        byte[] data = HttpTools.doGet(url);
        if (data != null) {
            try {
                String str = new String(data,"utf-8");
//                Log.d("clientAPI====",str);
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException | JSONException e) {
                e.printStackTrace();
            }
        }

        return ret;

    }

    // ----------------------
    // ----------------------
    // -----
    // 接口 17 专辑播放页
    public static JSONObject getAlbumDetail(long albumId){
        //  /mobile/others/ca/album/track/203355/true/1/20?device=android&pageSize=20&albumId=203355&isAsc=true
        JSONObject ret = null;
        String url = API_POINT+"/mobile/others/ca/album/track/"+albumId+"/true/1/20?device=android&page" +
                "Size=20&albumId=" +albumId+
                "&isAsc=true";
        byte[] data = HttpTools.doGet(url);
        if (data != null) {
            try {
                String str = new String(data,"utf-8");
//                Log.d("clientAPI====",str);
                ret = new JSONObject(str);
            } catch (UnsupportedEncodingException | JSONException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }


    // -----------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------
    // ----------------------

}
